<?php

/**
 * 短信发送
 * File Name：Sns.php
 * File Encoding：UTF-8
 * File New Time：2014-8-1 9:40:08
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class Sns {

    //接口地址
    protected $serverUrl = 'http://sdk.entinfo-----.cn:8060/webservice.asmx';
    protected $serverUrlSpare = 'http://sdk2.entinfo-----.cn:8060/webservice.asmx';
    //需要发送的手机号码
    protected $mobile = array();
    //短信内容
    protected $content = NULL;
    //错误信息
    protected $error = NULL;
    //发送内容
    protected $fields = array(
        'sn' => 'SDK-ZRG-010-04139',
    );

    /**
     * 链接短信服务
     * @staticvar null $handier
     * @return \Sns
     */
    static public function getInstance() {
        static $handier = NULL;
        if (empty($handier)) {
            $handier = new Sns();
        }
        return $handier;
    }

    /**
     * 获取最近的一条错误信息
     * @return string
     */
    public function getError() {
        return $this->error;
    }

    /**
     * 发送短信
     * @param string $moblie 手机号
     * @param string $content 短信内容
     * @return boolean
     */
    public function send($moblie = '', $content = '') {
        return true;
        if (empty($moblie)) {
            if (empty($this->mobile)) {
                $this->error = '没有需要发送的手机号码！';
                return false;
            } else {
                $moblie = implode(',', $this->mobile);
                $this->mobile = array();
            }
        }
        if (empty($content)) {
            if (empty($this->content)) {
                $this->error = '没有需要发送的短信内容！';
                return false;
            } else {
                $content = $this->content;
                $this->content = NULL;
            }
        }
        $this->setFields($moblie, $content);
        $Curl = new Curl();
        return $Curl->post($this->serverUrl . '/mt', $this->fields);
    }

    /**
     * 设置需要发送的手机号码
     * @param type $moblie 字符串时，多个用英文都好间隔，或者一个一维数组
     * @return boolean
     */
    public function mobile($moblie) {
        if (empty($moblie)) {
            return $this;
        }
        if (is_string($moblie)) {
            $moblie = explode(',', $moblie);
        }
        $this->mobile = $moblie;
        return $this;
    }

    /**
     * 追加一个手机号
     * @param type $moblie 手机号码
     * @return boolean
     */
    public function add($moblie) {
        if (empty($moblie) || !is_string($moblie)) {
            return $this;
        }
        if (!in_array($moblie, $this->mobile)) {
            $this->mobile[] = $moblie;
        }
        return $this;
    }

    /**
     * 设置短信内容
     * @param type $content 短信内容
     * @return boolean
     */
    public function data($content) {
        if (empty($content)) {
            return false;
        }
        $this->content = $content;
        return $this;
    }

    /**
     * 设置待发送字段内容
     * @param type $moblie 手机号码
     * @param type $content 短信内容
     * @return boolean
     */
    protected function setFields($moblie, $content) {
        $this->fields['pwd'] = strtoupper(md5($this->fields['sn'] . '097278'));
        $this->fields['mobile'] = $moblie;
        $this->fields['content'] = iconv("utf-8", "gb2312//IGNORE", $content . "\n(www.1zw.com)【1折网】");
        $this->fields['ext'] = '';
        $this->fields['stime'] = '';
        $this->fields['rrid'] = '';
        return true;
    }

}
